package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.safe.SafeUtil;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.MemberCardService;

/**
 * 后台用户管理
 *
 * @author zhaigt
 */
@Controller
@RequestMapping("/member")
public class MemberCtrl extends BaseCtrl {
	@Autowired
	private MemberCardService memberCardService;
	// 首页显示新通知数量
	@RequestMapping("/queryIndexInfo")
	@ResponseBody
	public Map<String, Object> queryIndexInfo() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 后台首页查询未审核人员数量
		jsonMap.put("count", memberService.count("userType","1","status","1"));
		return jsonMap;
	}

	/**
	 * 初始化
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String userType) {
		return new ModelAndView("1".equals(userType)?"/member/corpList.jsp":"/member/personList.jsp");
	}

	/**
	 * 查询
	 *
	 * @param member
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Member member, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", memberService.query(member, fromDate, toDate));
			Long count = memberService.count(member, fromDate, toDate);
			jsonMap.put("iTotalDisplayRecords", count);
			jsonMap.put("iTotalRecords", count);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询错误", e);
		}
		return jsonMap;
	}

	@RequestMapping("/add")
	public ModelAndView add(String userType) {
		return new ModelAndView("1".equals(userType)?"/member/corpEdit.jsp":"/member/personEdit.jsp");
	}
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		ModelAndView mv = new ModelAndView();
		Member member = this.memberService.find(id);
		
		mv.addObject("memberCard", memberCardService.findOne("memberid",member.getId()));
		if(member != null) {
			mv.setViewName("1".equals(member.getUserType())?"/member/corpEdit.jsp":"/member/personEdit.jsp");
			mv.addObject("data", member);
		}
		return mv;
	}

	@RequestMapping("/oper")
	@ResponseBody
	public Map<String, Object> oper(String id, int type, String value) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			String result = this.memberService.updateBatch(id, type, value);
			jsonMap.put("statusCode", 200);
			if(result != null) jsonMap.put("info", result);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "会员批量更新异常！", e);
		}
		return jsonMap;
	}

	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Member entity) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(entity.getId() == null || entity.getId().length()==0) {
				entity.setId(null);
				entity.setPhoto("/images/no-img_mid_.jpg");
				entity.setCreateTime(DateUtil.getCurrentTime());
				entity.setPassword(SafeUtil.md5(entity.getUserName()).substring(8, 16));
				this.memberService.save(entity);
			}else{
		 //用户等级   0运营商，5是1星运营商，6是2星运营商，7是3星运营商,8是4星运营商,9是5星运营商

				Member mbrd=memberService.find(entity.getId());
				if(!mbrd.getUserLevel().equals(entity.getUserLevel())){
					if(entity.getUserLevel()!=null&&entity.getUserLevel().equals("0")){
						 List<Map<String,Object>> list =	memberService.queryshangjin(entity.getId());
						 for (int i = 0; i < list.size(); i++) {
							String id= (String) list.get(i).get("id");
							Member mdid=memberService.find(id);
						String[] dlist=mdid.getTdlist().split(id);
						String tdlist="";
						if(dlist.length>1){
							tdlist=entity.getId()+","+dlist[1];
						}else{
							tdlist=entity.getId();
						}
							mdid.setTdlist(tdlist);
							mdid.setTuandui(entity.getId());
							memberService.update(mdid);
						}
					}
				}
				entity.setTuandui("");
			this.memberService.updateWithValidator(entity);
			jsonMap.put("statusCode", 200);
			
			}
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "会员批量更新异常！", e);
		}
		return jsonMap;
	}

	/**
	 * 下载Excel
	 *
	 * @return
	 */
	@RequestMapping("/downExcel")
	public void downExcel(Member member, HttpServletResponse response) {
		List<Member> list = this.memberService.query(member);
		String[] titles, fieldNames;
		String name;
		if("0".equals(member.getUserType())) {
			name = "个人会员";
			titles = new String[]{"用户名", "手机号", "状态", "认证", "注册时间", "余额"};
			fieldNames = new String[]{"userName", "mobile", "status", "approve", "createTime", "money"};
		} else {
			name = "企业会员";
			titles = new String[]{"用户名", "状态", "认证", "企业名称", "注册时间", "余额"};
			fieldNames = new String[]{"userName", "status", "approve", "company.companyName", "createTime", "money"};
		}
		super.downExcel(list, titles, fieldNames, name, response);
	}
public static void main(String[] args) {
	System.out.println("402881196735c3cd016736540d3b0008,402881196735c3cd016736a717d8000c,402881196736ec60016736efd8760000,402881196736fc7f016738a263380002,402881196736ec60016736efd8760001".split("402881196736fc7f016738a263380002").length);
}
}